﻿#include "QPet.h"
#include <QLabel>
#include <QDir>
 
Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
	QString appPath = qApp->applicationDirPath();
	QDir::setCurrent(appPath);

	QIcon icon("heart.png");
	setWindowIcon(icon);
	setContextMenuPolicy(Qt::NoContextMenu);
	setAttribute(Qt::WA_TranslucentBackground);
	setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
	setContextMenuPolicy(Qt::ActionsContextMenu);

	QSize size(800, 800);
	resize(size);

	flashAction = new QAction(tr("Flash"), this);
	addAction(flashAction);
	connect(flashAction, &QAction::triggered, this, &Widget::FlashGif);

	nextAction = new QAction(tr("Next"), this);
	addAction(nextAction);
	connect(nextAction, &QAction::triggered, this, &Widget::nextGif);

	quitAction = new QAction(tr("&Quit"), this);
	addAction(quitAction);
	connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

	trayIconMenu = new QMenu(this);
	trayIconMenu->addAction(flashAction);
	trayIconMenu->addAction(nextAction);
	trayIconMenu->addAction(quitAction);

	trayIcon = new QSystemTrayIcon(this);
	connect(trayIcon, &QSystemTrayIcon::activated, this, &Widget::iconActivated);

	trayIcon->setContextMenu(trayIconMenu);
	trayIcon->setIcon(icon);
	trayIcon->show();

	QLabel* label = new QLabel(this);
	label->resize(size);

	movie = new QMovie;
    label->setMovie(movie);

	FlashGif();
	nextGif();
}

Widget::~Widget()
{
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton)
    {
        if (dragging)
        {
            //delta 相对偏移量
            QPoint delta = event->globalPos() - startPosition;
            //新位置：窗体原始位置+偏移量
            move(framePosition + delta);
        }
    }

    QWidget::mouseMoveEvent(event);
}
 
void Widget::mousePressEvent(QMouseEvent *event)
{
    //响应左键
    if (event->button() == Qt::LeftButton)
    {
        dragging = true;
        startPosition = event->globalPos();
        framePosition = frameGeometry().topLeft();
    }

    QWidget::mousePressEvent(event);
}
 
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    dragging = false;
    QWidget::mouseReleaseEvent(event);
}

void Widget::closeEvent(QCloseEvent *event)
{
	if (trayIcon->isVisible()) {
		hide();
		event->ignore();
	}
}

void Widget::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
	switch (reason) {
	case QSystemTrayIcon::Trigger:
	case QSystemTrayIcon::DoubleClick:
		if (!isVisible())
		{
			showNormal();
		}
		break;
	default:
		;
	}
}

void Widget::nextGif()
{
	if (!gifs.empty())
	{
		QString gif = movie->fileName();
		auto iterFind = qFind(gifs.begin(), gifs.end(), gif);
		if (iterFind != gifs.end())
		{
			++iterFind;
		}

		if (iterFind == gifs.end())
		{
			iterFind = gifs.begin();
		}

		movie->stop();
		movie->setFileName(*iterFind);
		movie->start();
	}
}

void Widget::FlashGif()
{
	gifs.clear();

	QDir dir(qApp->applicationDirPath());
	dir.cd("gif");

	QStringList nameFilters;
	nameFilters << "*.gif";

	auto files = dir.entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
	for each(auto file in files)
	{
		gifs.push_back(file.absoluteFilePath());
	}
}